<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\Score;
use App\Models\Year;
use App\Models\Config;
use App\Models\User;

class ScoreCreate extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'score:create';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $year =  Year::where('value',date('Y'))->first();

        if($year && isset($year->user_score[0]) && (!isset($year->score))) {

            $users = User::whereHas('roles', function ($q) {
                $q->whereNotIn('name', ['admin', 'director', 'deputy_director']);
            })->get();

            foreach ($users as $user) {
                if ($user->roles[0]->name == 'staff') {
                    $config = Config::where('name', 'staff')->first()->value;

                    $staff_sum = $this->role_score($user,$year->id,['director'],$config[0] / 100);

                    $staff_sum += $this->role_score($user,$year->id,['deputy_director'],$config[1] / 100);

                    $staff_sum += $user->year_score()->where('year_id', $year->id)->whereHas('user.roles', function ($q) {
                        $q->whereIn('name', ['principal', 'main_principal']);
                    })->whereNotIn('user_id',$this->main_principal($user->department_id))
                        ->avg('value') * ($config[2] / 100);

                    $staff_sum += $user->year_score()->where('year_id', $year->id)->whereHas('user.roles', function ($q) {
                            $q->whereIn('name', ['main_principal']);
                        })->whereHas('user', function ($q) use ($user) {
                            $q->where('department_id', $user->department_id);
                        })->avg('value') * ($config[3] / 100);

                    $staff_sum += $this->role_score($user,$year->id,['staff'],$config[4] / 100);
                } else {
                    $config = Config::where('name', 'principal')->first()->value;

                    $staff_sum = $this->role_score($user,$year->id,['director'],$config[0] / 100);

                    $staff_sum += $this->role_score($user,$year->id,['deputy_director'],$config[1] / 100);

                    $staff_sum += $this->role_score($user,$year->id,['principal','main_principal'],$config[2] / 100);

                    $staff_sum += $this->role_score($user,$year->id,['staff'],$config[3] / 100);
                }

                Score::create([
                    'user_id' => $user->id,
                    'year_id' => $year->id,
                    'value' => round($staff_sum, 2),
                ]);
            }

            $director_users = User::whereHas('roles', function ($q) {
                $q->whereIn('name', ['director', 'deputy_director']);
            })->get();

            $year_score_avg = Score::with('user')->where('year_id',$year->id)->whereHas('user.roles', function ($q) {
                $q->whereIn('name', ['principal', 'main_principal']);
            })->avg('value');

            foreach ($director_users as $v) {
                Score::create([
                    'user_id' => $v->id,
                    'year_id' => $year->id,
                    'value' => round($year_score_avg, 2),
                ]);
            }
        }
    }

    public function main_principal($department_id)
    {
        return User::where('department_id', $department_id)->whereHas('roles', function ($q) {
            $q->where('name', 'main_principal');
        })->pluck('id');
    }

    public function role_score($user,$year_id,$role,$config)
    {
        return $user->year_score()->where('year_id', $year_id)->whereHas('user.roles', function ($q) use($role) {
                $q->whereIn('name', $role);
            })->avg('value') * $config;
    }

}
